package org.me.marble;

import java.nio.*;

import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class QuestionMarkObject extends AvailableObject{
    private FloatBuffer frontBuffer,backBuffer,outerBlanketBuffer,innerBlanketBuffer;
    private FloatBuffer cube1,cube2;
    private float res;
    private int size;

    public QuestionMarkObject(int id){
        super(id);
    }

    @Override
    public void effect(GameRenderer _renderer,MoveMarble _mm){
        double rand=Math.random();
        if(rand<0.2){
            _mm.modifySpeed(0.5);
            _renderer.addRunnable(timerTypes.slow.ordinal(),delay);
        }
        else if(rand>=0.2 && rand<0.4){
            _mm.modifySpeed(2);
            _renderer.addRunnable(timerTypes.fast.ordinal(),delay);
        }
        else if(rand>=0.4 && rand<0.6){
            _renderer.invert();
            _renderer.addRunnable(timerTypes.invert.ordinal(),delay);
        }
        else if(rand>=0.6 && rand<0.8){
            OptionsLibrary.points+=600;
        }
        else if(rand>=0.8){
            OptionsLibrary.lifes++;
        }
    }

    @Override
    public void init(){
        if(OptionsLibrary.objects_quality.equals("low"))
            res=12;
        else if(OptionsLibrary.objects_quality.equals("medium"))
            res=24;
        else if(OptionsLibrary.objects_quality.equals("high"))
            res=36;

        ArrayList<Float> front_circle=new ArrayList<Float>();
        ArrayList<Float> back_circle=new ArrayList<Float>();
        ArrayList<Float> outerblanket=new ArrayList<Float>();
        ArrayList<Float> innerblanket=new ArrayList<Float>();

        float outer_rad=0.2f,inner_rad=0.1f;
        float x0=0.0f,y0=0.22f;
        for(int i=0;i<=res*7/12;++i){
            float a=x0+outer_rad*(float)Math.cos(Math.PI*(1-2*i/res));
            float b=y0+outer_rad*(float)Math.sin(Math.PI*(1-2*i/res));
            float c=x0+inner_rad*(float)Math.cos(Math.PI*(1-2*i/res));
            float d=y0+inner_rad*(float)Math.sin(Math.PI*(1-2*i/res));

            front_circle.add(a);
            front_circle.add(b);
            front_circle.add(-0.05f);
            front_circle.add(c);
            front_circle.add(d);
            front_circle.add(-0.05f);

            back_circle.add(0,a);
            back_circle.add(1,b);
            back_circle.add(2,0.05f);
            back_circle.add(3,c);
            back_circle.add(4,d);
            back_circle.add(5,0.05f);
        }

        outer_rad=0.2f;
        inner_rad=0.3f;
        x0+=0.4f*(float)Math.cos(Math.PI/3);
        y0+=-0.4f*(float)Math.sin(Math.PI/3);
        for(int i=(int)res*1/6;i>=0;--i){
            float a=x0+outer_rad*(float)Math.cos(Math.PI*(1-2*i/res));
            float b=y0+outer_rad*(float)Math.sin(Math.PI*(1-2*i/res));
            float c=x0+inner_rad*(float)Math.cos(Math.PI*(1-2*i/res));
            float d=y0+inner_rad*(float)Math.sin(Math.PI*(1-2*i/res));
            
            front_circle.add(a);
            front_circle.add(b);
            front_circle.add(-0.05f);
            front_circle.add(c);
            front_circle.add(d);
            front_circle.add(-0.05f);

            back_circle.add(0,a);
            back_circle.add(1,b);
            back_circle.add(2,0.05f);
            back_circle.add(3,c);
            back_circle.add(4,d);
            back_circle.add(5,0.05f);
        }

        innerblanket.add(front_circle.get(0));
        innerblanket.add(front_circle.get(1));
        innerblanket.add(front_circle.get(2));
        innerblanket.add(back_circle.get(back_circle.size()-6));
        innerblanket.add(back_circle.get(back_circle.size()-5));
        innerblanket.add(back_circle.get(back_circle.size()-4));
        for(int i=0;i<front_circle.size()/6;++i){
            outerblanket.add(front_circle.get(front_circle.size()-i*6-6));
            outerblanket.add(front_circle.get(front_circle.size()-i*6-5));
            outerblanket.add(front_circle.get(front_circle.size()-i*6-4));
            outerblanket.add(back_circle.get(i*6));
            outerblanket.add(back_circle.get(i*6+1));
            outerblanket.add(back_circle.get(i*6+2));

            innerblanket.add(front_circle.get(i*6+3));
            innerblanket.add(front_circle.get(i*6+4));
            innerblanket.add(front_circle.get(i*6+5));
            innerblanket.add(back_circle.get(back_circle.size()-i*6-3));
            innerblanket.add(back_circle.get(back_circle.size()-i*6-2));
            innerblanket.add(back_circle.get(back_circle.size()-i*6-1));
        }
        innerblanket.add(front_circle.get(front_circle.size()-6));
        innerblanket.add(front_circle.get(front_circle.size()-5));
        innerblanket.add(front_circle.get(front_circle.size()-4));
        innerblanket.add(back_circle.get(0));
        innerblanket.add(back_circle.get(1));
        innerblanket.add(back_circle.get(2));
        
        size=front_circle.size()/3;

        float[] cube_one={x0-outer_rad,y0-0.05f,0.05f,
                          x0-inner_rad,y0-0.05f,0.05f,
                          x0-outer_rad,y0-0.15f,0.05f,
                          x0-inner_rad,y0-0.15f,0.05f,
                          x0-outer_rad,y0-0.15f,-0.05f,
                          x0-inner_rad,y0-0.15f,-0.05f,
                          x0-outer_rad,y0-0.05f,-0.05f,
                          x0-inner_rad,y0-0.05f,-0.05f
        };
        float[] cube_two={x0-outer_rad,y0-0.15f,0.05f,
                          x0-outer_rad,y0-0.15f,-0.05f,
                          x0-outer_rad,y0-0.05f,0.05f,
                          x0-outer_rad,y0-0.05f,-0.05f,
                          x0-inner_rad,y0-0.05f,0.05f,
                          x0-inner_rad,y0-0.05f,-0.05f,
                          x0-inner_rad,y0-0.15f,0.05f,
                          x0-inner_rad,y0-0.15f,-0.05f
        };

        frontBuffer=BufferFactory.MakeFloatBuffer(front_circle);
        backBuffer=BufferFactory.MakeFloatBuffer(back_circle);
        outerBlanketBuffer=BufferFactory.MakeFloatBuffer(outerblanket);
        innerBlanketBuffer=BufferFactory.MakeFloatBuffer(innerblanket);
        cube1=BufferFactory.MakeFloatBuffer(cube_one);
        cube2=BufferFactory.MakeFloatBuffer(cube_two);

        color=new MyColor();
        color.setAmbient(new float[]{1.0f,1.0f,0.0f,0.8f});
        color.setDiffuse(new float[]{1.0f,1.0f,0.0f,0.8f});
        color.setSpecular(new float[]{0.6f,0.6f,0.6f,0.7f});
        color.setEmission(new float[]{0.1f,0.1f,0.1f,0.6f});
        color.setShininess(1.0f);
    }

    @Override
    public void draw(GL10 gl){
        transform(gl);

        OpenGLSystem.enable(gl,OpenGLSystem.CULL_FACE | OpenGLSystem.BLEND_IF_NO_FOG);
        OpenGLSystem.enableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | OpenGLSystem.VERTEX_ARRAY);

        OpenGLSystem.setMaterial(gl,color);

        OpenGLSystem.drawTriangles(gl,null,frontBuffer,frontBuffer,
                null,size,GL10.GL_TRIANGLE_STRIP);
        OpenGLSystem.drawTriangles(gl,null,innerBlanketBuffer,innerBlanketBuffer,
                null,size+2,GL10.GL_TRIANGLE_STRIP);
        OpenGLSystem.drawTriangles(gl,null,outerBlanketBuffer,outerBlanketBuffer,
                null,size,GL10.GL_TRIANGLE_STRIP);
        OpenGLSystem.drawTriangles(gl,null,backBuffer,backBuffer,
                null,size,GL10.GL_TRIANGLE_STRIP);
        OpenGLSystem.drawTriangles(gl,null,cube1,cube1,
                null,8,GL10.GL_TRIANGLE_STRIP);
        OpenGLSystem.drawTriangles(gl,null,cube2,cube2,
                null,8,GL10.GL_TRIANGLE_STRIP);

        OpenGLSystem.disableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | OpenGLSystem.VERTEX_ARRAY);
        OpenGLSystem.disable(gl,OpenGLSystem.CULL_FACE | OpenGLSystem.BLEND);
    }
}